package action;

import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import models.IssueData;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class test2Action extends ActionSupport{

	/**
	 * 
	 */	
	private String commandButton;
	public String getCommandButton() {
		return commandButton;
	}
	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}
	private String username="";
	private String pass="";
	private String email;
	private String email_matter;
	private static final long serialVersionUID = 1L;
	private String host = "smtp.gmail.com";
	private String from = "issue.tracker19@gmail.com";
		private String password = "tracker19";
	   private String to;
	   private String subject = "jsp email testing";
	   private String body = "hi....test successful";
	   
	   static Properties properties = new Properties();
	   static
	   {
		  properties.put("mail.smtp.starttls.enable", "true");
	      properties.put("mail.smtp.host", "smtp.gmail.com");
	     
	      properties.put("mail.smtp.user", "issue.tracker19@gmail.com");
	      properties.put("mail.smtp.password", "tracker19");
	      
	      properties.put("mail.smtp.socketFactory.port", "465");
	      properties.put("mail.smtp.socketFactory.class",
	                     "javax.net.ssl.SSLSocketFactory");
	      properties.put("mail.smtp.auth", "true");
	    //  properties.put("mail.smtp.port", "465");
	      
	     
	      properties.put("mail.smtp.port", "587");
	     // properties.put("mail.smtp.auth", "true");
	      
	   }

	   public String execute() 
	   {
		   
		   if(commandButton.startsWith("mark as resolved"))
		   {
			   Map<String, Object> session = ActionContext.getContext().getSession();
				IssueData issue = (IssueData)session.get("Issue");
			   issue.updateIssueResolve(issue.getIssue_num());
			   ArrayList<IssueData> IssueList = IssueData.find("");
		    	System.out.println("inlogacc  "+IssueList.get(0).getAssignedto());
		    	//session.put("TagList", TagList );
		    	session.put("IssueList",IssueList);
			   return "success";
		   }
	   else if(commandButton.startsWith("send email"))
		   {
	      String ret = SUCCESS;
	      try
	      {
	    	  to = email;
	    	//  to = "sreeraag.sss@gmail.com";
	    	  Session session = Session.getDefaultInstance(properties, null);
	    	  MimeMessage message = new MimeMessage(session);
	    	  message.setFrom(new InternetAddress(from));
	    	  InternetAddress toAddress = new InternetAddress(to);
	    	  // To get the array of addresses
	    	  //for( int i=0; i < to.length; i++ ) { // changed from a while loop
	    	  //toAddress[i] = new InternetAddress(to[i]);
	    	  //}
	    	  System.out.println(Message.RecipientType.TO);
	    	  //for( int i=0; i < toAddress.length; i++) { // changed from a while loop
	    	  message.addRecipients(Message.RecipientType.TO, to);
	    	  //}
	    	  message.setSubject("issue-reg");
	    	  message.setText(email_matter);
	    	  Transport transport = session.getTransport("smtp");
	    	  transport.connect(host, from, password);
	    	  transport.sendMessage(message, message.getAllRecipients());
	    	  transport.close();
	      }
	      
	      catch(Exception e)
	      {
	         ret = ERROR;
	         e.printStackTrace();
	      }
	      return ret;
		   }
		   
		   
		   return "success";
		   
	   }
	   public String getPass() {
			return pass;
		}

		public void setPass(String pass) {
			this.pass = pass;
		}

		public String getUsername() {
			return username;
		}

		public void setUsername(String username) {
			this.username = username;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}
		public String getEmail() {
			return email;
		}
		public void setEmail(String email) {
			this.email = email;
		}
		public String getEmail_matter() {
			return email_matter;
		}
		public void setEmail_matter(String email_matter) {
			this.email_matter = email_matter;
		}
   
}





//String[] to = {this.email,this.aemail}; // added this line
